﻿using System;
using System.Configuration;

namespace FSAG.UserImporter
{
    /// <summary>
    /// Class ConfigManager used as helper class for getting settings from the app.config file
    /// </summary>
    public static class ConfigManager
    {
        /// <summary>
        /// General instance to the Log4Net logger
        /// </summary>
        public static readonly log4net.ILog Log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

        /// <summary>
        /// Used to get settings from the app.config file based on the given parameter
        /// </summary>
        /// <param name="parameter"></param>
        /// <param name="isNecessary"></param>
        /// <returns></returns>
        public static string GetSetting(string parameter, bool isNecessary = true)
        {
            var value = ConfigurationManager.AppSettings.Get(parameter);

            if (value == null)
            {
                throw new ApplicationException("Necessary configurationparameter " + parameter + (" not found."));
            }
            return ConfigurationManager.AppSettings.Get(parameter);
        }

        /// <summary>
        /// Used to get numerical value from the app.config file based on the given parameter
        /// </summary>
        /// <param name="parameter"></param>
        /// <param name="isNecessary"></param>
        /// <returns></returns>
        public static int GetSettingAsNumber(string parameter, bool isNecessary = true)
        {
            var setting = GetSetting(parameter, isNecessary);
            int settingAsNumber;

            var result = Int32.TryParse(setting, out settingAsNumber);

            if (result)
            {
                return settingAsNumber;
            }
            throw new ApplicationException("Numerical value of configurationparameter:  " + parameter + (" expected."));
        }
    }
}
